package com.foreveross.mofang.serviceconsole.core;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 执行频率。当如果勾选重复时，TYPE才会生成效，否则按固定时间执行。如果填写了repeatTime，则那天按此频段执行
 * 任务
 * 
 * @author fb
 */
@Embeddable
public class Frequency implements Serializable {

	/**    
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// 是否重复
	@NotNull(message = "是否重复不能为空")
	@Column(name = "ISAGAIN")
	private Boolean isagain = false;
	
	@NotNull(message = "重复类型不能为空")
	@Column(name = "TYPE")
	@Enumerated(EnumType.STRING)
	private FrequencyType type;
	
	@NotNull(message = "执行时间不能为空")
	@Column(name = "DATE")
	private Date date;
	
	//(默认分钟) 如果此值存在，则当天内按此值间隔执行
	@Column(name = "REPEAT_TIME")
	private Integer repeatTime;
	
	// 七位二进制字符串表示，isagain为true时有效，一周的重复频率，如 一周7天都重复则显示为：1111111，星期一和星期六不重复，则显示为0111101
	@Size(min = 7, max = 7, message = "按周重复数据不正确，只能是由0或1组成的7位字符串")
	@Column(name = "WEEK")
	private String week;
	
	public Boolean getIsagain() {
		return isagain;
	}

	public void setIsagain(Boolean isagain) {
		this.isagain = isagain;
	}

	public FrequencyType getType() {
		return type;
	}

	public void setType(FrequencyType type) {
		this.type = type;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Integer getRepeatTime() {
		return repeatTime;
	}

	public void setRepeatTime(Integer repeatTime) {
		this.repeatTime = repeatTime;
	}

	public String getWeek() {
		return week;
	}

	public void setWeek(String week) {
		this.week = week;
	}

	public static enum FrequencyType {
		DAY("按日重复"),
		WEEK("按周重复"),
		MONTH("按月重复"),
		QUARTER("按季重复");
		
		private String lable;
		
		FrequencyType (String lable) {
			this.lable = lable;
		}
		
		public String getLable() {
			return lable;
		}

		public void setLable(String lable) {
			this.lable = lable;
		}
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
